package io.renren.modules.news.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.news.entity.OpenAdvertEntity;
import io.renren.modules.news.service.OpenAdvertService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;


/**
 * 
 * 
 * @author wxy
 * @email sharonwang_xy@163.com
 * @date 2018-10-16 01:01:30
 */
@RestController
@RequestMapping("news/openadvert")
public class OpenAdvertController {
	@Autowired
	private OpenAdvertService openAdvertService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("news:openadvert:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<OpenAdvertEntity> openAdvertList = openAdvertService.queryList(query);
		int total = openAdvertService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(openAdvertList, total, query.getLimit(), query.getCurrPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("news:openadvert:info")
	public R info(@PathVariable("id") Integer id){
		OpenAdvertEntity openAdvert = openAdvertService.queryObject(id);
		
		return R.ok().put("openAdvert", openAdvert);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("news:openadvert:save")
	public R save(@RequestBody OpenAdvertEntity openAdvert){

		//新增默认状态1
		if(null==openAdvert.getState()||""==openAdvert.getState()){
			openAdvert.setState("1");
		}
		//创建时间
		if(null==openAdvert.getCreateTime()||""==openAdvert.getCreateTime()){
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			openAdvert.setCreateTime(df.format(new Date()));
	}
		openAdvertService.save(openAdvert);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("news:openadvert:update")
	public R update(@RequestBody OpenAdvertEntity openAdvert){
		openAdvertService.update(openAdvert);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("news:openadvert:delete")
	public R delete(@RequestBody Integer[] ids){
		openAdvertService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
